<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <title>工装工具准备查询报表</title>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData">
    <!-- 报表标题 -->
    <vue2-report-header :title="'工装工具准备查询报表'"></vue2-report-header>
    <div style="padding: 10px;">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules">
        <el-form-item label="组织结构:" prop="selectOrg">
          <el-cascader :options="orginazition" :props="{ checkStrictly: true}" ref="orginazitionRef"
            v-model="queryInfo.selectOrg" clearable style="width:300px;" @change="selectOrgChange">
          </el-cascader>
        </el-form-item>

        <el-form-item label="使用人:">
          <el-select v-model="queryInfo.appler" placeholder="请选择" clearable filterable style="width: 120px;">
            <el-option v-for="i in applerList" :key="i.employeeid" :label="i.fullname" :value="i.employeeid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop='productname' label="零件图号:">
          <el-autocomplete v-model.trim="queryInfo.productname" placeholder="请输入零件图号" :trigger-on-focus="false"
            style="width:240px;" :fetch-suggestions="searchProductAsync" :value-key="'productname'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='containername' label="流水卡号:">
          <el-autocomplete v-model.trim="queryInfo.containername" placeholder="请输入流水卡号" :trigger-on-focus="false"
            style="width:280px;" :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="申请工区:">
          <el-select v-model="queryInfo.applyworkcenterid" placeholder="请选择申请工区" clearable filterable>
            <el-option v-for="i in workcenterList" :key="i.workcenterid" :label="i.workcentername"
              :value="i.workcenterid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="申请时间:">
          <el-date-picker v-model="queryInfo.createdate" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>

        <el-form-item label="状态:">
          <el-select v-model="queryInfo.status" placeholder="请选择" clearable filterable style="width: 100px;">
            <el-option :key="'待准备'" :label="'待准备'" :value="'待准备'"></el-option>
            <el-option :key="'待配送'" :label="'待配送'" :value="'待配送'"></el-option>
            <el-option :key="'已配送'" :label="'已配送'" :value="'已配送'"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="类型:">
          <el-select v-model="queryInfo.tooltype" placeholder="请选择" clearable filterable style="width: 100px;">
            <el-option :key="'刀具'" :label="'刀具'" :value="'刀具'"></el-option>
            <el-option :key="'量具'" :label="'量具'" :value="'量具'"></el-option>
            <el-option :key="'夹模具'" :label="'夹模具'" :value="'夹模具'"></el-option>
            <el-option :key="'辅材'" :label="'辅材'" :value="'辅材'"></el-option>
          </el-select>
        </el-form-item>

        <el-popover placement="top-start" title="报表说明:" width="500" trigger="hover">
          <el-button icon="el-icon-info" slot="reference">报表说明</el-button>
          <p>1. 派工后MES系统会根据批次工序上关联的刀具、量具、夹模具、辅材等基础数据，自动发起生产准备申请，此时状态为【待准备】</p>
          <p>2. 在MES系统【生产准备】功能中，点击【配送确认】后，此时状态会更新为【已准备】</p>
          <p>3. 报表会过滤掉批次已关闭的数据。</p>
        </el-popover>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getData">查询</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格显示区 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column label="零件图号" prop="productname" width="140" show-overflow-tooltip></el-table-column>
        <el-table-column label="流水卡号" prop="containername" width="140" show-overflow-tooltip></el-table-column>
        <el-table-column label="主制工区" prop="fromworkcentername" width="100"></el-table-column>
        <el-table-column label="工序名称" prop="workflowstepname" width="80" show-overflow-tooltip></el-table-column>
        <el-table-column label="工序号" prop="specno" width="60"></el-table-column>
        <el-table-column label="工步名称" prop="stepinfoname" width="80" show-overflow-tooltip></el-table-column>
        <el-table-column label="工步号" prop="stepno" width="60"></el-table-column>
        <el-table-column label="工具编号" prop="toolno" width="130" show-overflow-tooltip></el-table-column>
        <el-table-column label="工具名称" prop="toolname" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="编码/规格" prop="drawingno" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="类型" prop="tooltype" width="80">
          <template slot-scope="scope" v-if="scope.row.tooltype">
            <el-tag>{{scope.row.tooltype}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="需求数量" prop="needqty" width="80"></el-table-column>
        <el-table-column label="需求日期" prop="needdate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="申请工区" prop="workcentername" width="100"></el-table-column>
        <el-table-column label="班组" prop="teamname" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="使用人" prop="useperson" width="80">
          <template slot-scope="scope" v-if="scope.row.useperson">
            <el-tag>{{scope.row.useperson}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" prop="createdate" width="100" :formatter="splitDate"></el-table-column>
        <el-table-column label="准备情况" prop="toolstatus" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.toolstatus === '已配送'" type="success">{{scope.row.toolstatus}}</el-tag>
            <el-tag v-else>{{scope.row.toolstatus}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="超期时间" prop="delaydays" width="80"></el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[12,50,100]" :page-size="queryInfo.pageSize"
        layout="prev,pager,next,jumper,sizes,total" :total="total">
      </el-pagination>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>
    </div>
  </div>

  <script type="module">
    import { getParams, fetchSuggestion, exportExcelData } from '../Scripts/hooks/useCommonMethod.js'
    import { getUserInfo, getWorkcenterList, getFactoryUsers, getMESOrginazition } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { splitDate } from '../Scripts/hooks/format.js'
    import Vue2ReportHeader from '../Scripts/components/Vue2ReportHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2ReportHeader },
      data: {
        splitDate,
        queryInfo: {
          selectOrg: [],
          appler: '',// 使用人
          productname: '',
          containername: '',
          status: '',
          tooltype: '',//类型
          applyworkcenterid: '',// 申请工区
          createdate: '',// 申请时间
          currentPage: 1,//当前页
          pageSize: 12//每页显示条数
        },
        total: 0,
        loading: false,
        loadingText: '数据加载中...',
        dataList: [],
        userInfo: {},
        orginazition: [],
        workcenterList: [],// 工区
        applerList: [], // 申请人
        // 表单验证
        rules: { selectOrg: [{ required: true, message: '组织结构不能为空', trigger: 'change' }] },
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] }
      },
      methods: {
        // 查询件号
        searchProductAsync(value, callback) {
          fetchSuggestion({ actionName: 'getProductnameAsync', name: 'productname', value, callback })
        },
        // 查询批次号
        searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback })
        },
        async selectOrgChange(val) {
          this.queryInfo.appler = null;
          this.queryInfo.applyworkcenterid = null;
          if (val) {
            const [factoryid] = val;
            this.applerList = await getFactoryUsers({ factoryid });
            this.workcenterList = await getWorkcenterList({ factoryid });
          }
        },
        //查询数据
        async getData() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) {
              return this.$message.error('必填项不能为空');
            } else {
              this.loading = true;
              this.loadingText = '数据加载中...';
              const { data: { data, meta, total } } = await axios.post('../handler/report/WorkToolsPrepareReport.ashx?action=getData', {
                type: 'getData',
                ...this.queryInfo
              })
              this.loading = false;
              if (meta.status === 500) { return this.$message.error(meta.msg); }
              this.dataList = data;
              this.total = total;
            }
          })
        },
        //导出excel
        async getExportExcel() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (!valid) {
              return this.$message.error('必填项不能为空');
            } else {
              const config = {
                url: '../handler/report/WorkToolsPrepareReport.ashx?action=exportExcel',
                data: { type: 'exportExcel', ...this.queryInfo }
              }
              exportExcelData(config, this);
            }
          })
        },
        handleCurrentChange(newPage) {
          this.queryInfo.currentPage = newPage;
          this.getData();
        },
        handleSizeChange(newSize) {
          this.queryInfo.pageSize = newSize;
          this.getData();
        }
      },
      async mounted() {
        this.userInfo = await getUserInfo({ employeeid: getParams("uid") });
        const { factoryid } = this.userInfo;
        this.orginazition = await getMESOrginazition({ factoryCode: '600|601|602|604|607|608|609' });
        if (this.orginazition.some(i => i.value === factoryid)) {
          this.queryInfo.selectOrg = [factoryid];
          this.applerList = await getFactoryUsers({ factoryid });
          this.workcenterList = await getWorkcenterList({ factoryid });
        }
      },
      watch: { queryInfo: { handler() { this.queryInfo.currentPage = 1 }, deep: true } }
    })
  </script>
</body>

</html>